<template>
    <div class="app-container">

        <div class="app-container-left">
            <tree ref="tree" @set-tree-data="setTreeData" />
        </div>

        <div class="app-container-right">

            <div :class="['filter-container',{'open':formState}]">
                <!-- <a v-if="screenState" class="form-more" href="#" title="更多筛选" @click="formState = !formState"><i
                        :class="formState ? 'el-icon-caret-top':'el-icon-caret-bottom'" /></a> -->
                <!-- <el-form ref="form" :model="form" @keyup.enter.native="handleFilter()" label-width="80px"> -->

                <!-- 筛选框需要些双份 一种是没有展开状态下的  只有主要的几个筛选框，另一种是全部的筛选框都展示 也就是展开状态下的 -->
                <!-- 没有展开状态下的筛选框 -->
                <el-form v-if="!formState" ref="filtrateForm" :inline="true" :model="filtrateForm" label-width="80px">

                    <el-form-item v-if="!formState">
                        <!-- <el-button class="filter-item" @click="handleFilter">
                            查询
                        </el-button> -->
                        <el-button v-permission="['sys:config:save']" type="primary" class="filter-item" style="margin-left: 10px;"
                            @click="handleCreateHouse1">充预存</el-button>

                    </el-form-item>
                </el-form>

                <!-- 展开状态下的筛选框 -->
                <el-form v-if="formState" ref="form" :inline="true" :model="filtrateForm" label-width="80px">

                    <!-- <el-form-item label="收款单号">
                        <el-input v-model.trim="filtrateForm.payCode" placeholder="请输入" style="width:160px;" clearable />
                    </el-form-item> -->

                </el-form>
                <div v-if="formState" class="filter-button">
                    <!-- <el-button class="filter-item" @click="handleFilter">
                        查询
                    </el-button> -->
                    <el-button v-permission="['sys:config:save']" type="primary" class="filter-item" style="margin-left: 10px;"
                        @click="handleCreateHouse">充预存</el-button>
                </div>

            </div>

            <div :class="['table',{'open':formState}]">
                <div class="table-container">
                    <!-- @selection-change="selectionChangeHandle" -->
                    <el-table :key="tableKey" v-loading="listLoading" row-key="menuId" height="100%" :data="list" border fit
                        highlight-current-row style="width: 100%;">

                        <!-- <el-table-column type="selection" header-align="center" align="center" width="50" /> -->

                        <el-table-column prop="itemsName" header-align="center" align="center" label="收费项目" />

                        <el-table-column prop="accountMoney" header-align="center" align="center" label="账户余额">
                            <template slot-scope="scope">{{ scope.row.accountMoney }}</template>
                        </el-table-column>

                        <!-- <el-table-column prop="updateTime" header-align="center" align="center" width="180" label="最后编辑时间">
                            <template slot-scope="scope">{{ scope.row.updateTime  }}</template>
                        </el-table-column>
                        <el-table-column prop="updateUserName" header-align="center" align="center" label="编辑人" />   -->
                        <el-table-column header-align="center" align="center" width="180" label="操作">
                            <template slot-scope="scope">
                                <el-button v-permission="['sys:config:update']" type="text" size="small"
                                    @click="handleCreateHouse(scope.row)">
                                    充预存
                                </el-button>
                                <el-button v-permission="['sys:config:update']" type="text" size="small"
                                    @click="handleUpdateHouse(scope.row)">
                                    退预存
                                </el-button>
                                <el-button v-permission="['finance:financesettlement:delete']" type="text" size="small"
                                    @click="deleteHandle(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>

                    </el-table>
                </div>
            </div>

            <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

        </div>

        <!-- 新增修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @getList="handleFilter" />

        <!-- 新增修改 -->
        <add-or-update1 v-if="addOrUpdateVisible1" ref="addOrUpdate1" @getList="handleFilter" />

    </div>
</template>

<script>

import { daleteHouseTree, getHouseList, daleteHouse } from '@/api/assets'// 请求数据
import { getFinanceprepaiditems, getChargeList, daleteFinanceprepaiditems } from '@/api/charge'// 请求数据  收费标准

import { HouseExportExcel, downloadMock, HouseImportExcel } from '@/api/index'// 请求数据
import permission from '@/directive/permission/index.js' // 权限判断指令
import { houseState, houseType, houseFunction } from '@/utils/enum' //字典
import { toDate } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination 分页组件
import Tree from './components/tree' // 树形组件
import AddOrUpdate from './add-or-update' // 新增或者修改弹框
import AddOrUpdate1 from './add-or-update1' // 新增或者修改弹框




export default {
    name: 'LayoutTable',
    components: { Pagination, Tree, AddOrUpdate, AddOrUpdate1 },
    directives: { permission }, // 指令
    // 过滤 {{row.status | statusFilte}}
    // html代码中，{{}}中实现filters顺序是 {{参数 | filters函数 | filters函数 | ...}}
    // 后面可以多个filters函数，前面filters函数return的结果是后面filters函数的参数。
    // 并且可以在过滤函数后面加上（）里面加上其他的参数。如 filterTwo('同学')
    filters: {
        toData (data) {
            return toDate(data)
        },
        // typeFilter (type) {
        //     return calendarTypeKeyValue[type]
        // },
        // houseStateFilter (status) {
        //     let map = houseState;
        //     return map[status]
        // }
    },
    data () {
        return {
            screenState: true, // 筛选框是否展示下拉按钮
            formState: false, // 筛选框的 开合
            // 筛选框的 form 表单
            filtrateForm: {
                payCode: '',//收款单号
                customerName: '',//客户名称
                itemsId: '',//项目id
                beginTime_endTime_pay: ['', ''],//收款日期
                beginTime_endTime_bill: ['', ''],//账单日期
            },
            rules: { // 表单验证
            },
            dataListSelections: [], // 批量删除选中
            // 树形选择结果
            treeData: {
                id: '', //房屋id
                name: '',
                parkId: '',//园区id
                depth: '',
                code: ''
            },
            tableKey: 0,
            list: [], // 表格数据
            total: 0, // 总条数
            listLoading: true, // 表格加载数据时的 loading
            listQuery: { // 表格当前页 和 当前页的展示数量
                page: 1,
                limit: 20
            },
            addOrUpdateVisible: false,
            addOrUpdateVisible1: false,
        }
    },
    computed: {
        // houseFunction_ () {
        //     let arr = [{ lable: '全部', value: '' }]
        //     for (let key in houseFunction) {
        //         arr.push({
        //             lable: houseFunction[key],
        //             value: key
        //         })
        //     }
        //     return arr
        // }
    },
    created () {
        this.getList()
    },
    methods: {
        // 树选择
        setTreeData (data) {
            if (!data.name) return;
            console.log('树形选择结果', data)
            this.treeData.id = data.id
            this.treeData.parkId = data.parkId
            this.treeData.type = data.type
            // this.treeData.name = data.name
            // this.treeData.depth = data.depth
            // this.treeData.code = data.code
            this.handleFilter()
        },
        // 获取表格数据
        async getList () {
            this.listLoading = true
            // 筛选+分页
            const params = Object.assign({
                houseIds: this.treeData.id
            }, this.listQuery)
            // 请求
            const res = await getFinanceprepaiditems(params).catch(e => { })
            // 失败
            if (!res) { this.listLoading = false; return }
            // 成功
            const { data, code, message } = res
            console.log(data)

            this.list = data.list
            this.total = data.totalCount
            this.listLoading = false
        },
        // 重新获取表格数据 页码归为1
        handleFilter () {
            this.listQuery.page = 1
            this.getList()
        },
        //刷新tree
        getTree () {
            this.$refs.tree.getTree()
        },

        // 新增数据-打开弹框
        handleCreateHouse1 (param) {
            if (!this.treeData.parkId) {
                this.$message({
                    message: '请先选择房屋',
                    type: 'warning',
                    duration: 1000,
                    onClose: () => {
                    }
                })
                return
            };
            this.addOrUpdateVisible1 = true
            let params = Object.assign({
                parkId: this.treeData.parkId,
                houseId: this.treeData.id,
            }, param)
            this.$nextTick(() => {
                this.$refs.addOrUpdate1.init('create', params)
            })
        },
        // 新增数据-打开弹框
        handleCreateHouse (row) {
            this.addOrUpdateVisible = true
            let params = Object.assign({
            }, row)
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init('create', params)
            })
        },
        // 修改 -- 打开弹框
        handleUpdateHouse (row) {
            this.addOrUpdateVisible = true
            let params = Object.assign({
            }, row)
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init('update', params)
            })
        },
        // 批量删除数据 - 多选
        selectionChangeHandle (val) {
            this.dataListSelections = val
        },
        // 删除
        deleteHandle (data) {
            console.log(this.dataListSelections, data)
            const idS = data ? [data.id] : this.dataListSelections.map(item => { return item.id })
            const nameS = data ? [data.itemsName] : this.dataListSelections.map(item => { return item.itemsName })
            this.$confirm(`确定对[${nameS.join('、')}]进行[删除]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                // 提交删除
                console.log(idS)
                const { code, message } = await daleteFinanceprepaiditems(idS)
                // 成功
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.handleFilter()// 刷新数据
                    }
                })
            })
        },

    }
}
</script>
<style lang="scss" scoped>
//表格布局的注释：
//$rowLength 这个变量表示了 筛选的搜索框会有多少行，由于在不同宽度显示器下 所占行数会不一定 ，所以下面使用媒体查询 做兼容
//最小兼容至 1366
@import '~@/styles/layout-table.scss';
$rowLength: 2;
@media all and (min-width: 1659px) {
    $rowLength: 2;
    @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
    $rowLength: 2;
    @include calculation($rowLength: $rowLength);
}
</style>
